package com.viphuli.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import butterknife.Bind;

import com.loopj.android.http.RequestParams;
import com.viphuli.app.R;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.part.HomeRoomServiceItem;
import com.viphuli.http.handler.MyBaseHttpResponseHandler.HandlerType;

/**
 * 上门体检
 * 
 * @author li.li
 *
 */
public class HomeServiceFragment extends BaseProgressFragment implements OnItemClickListener {

	@Bind(R.id.id_main_home_service_listview)
	protected ListView listLiew;
	private int serviceType;

	private BaseAdapter adapter;

	private List<HomeRoomServiceItem> list = new ArrayList<HomeRoomServiceItem>();

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_home_service;
	}

	@Override
	protected void initParams() {
		//解析参数
		Bundle args = getArguments();
		serviceType = args.getInt("service_type");
	}

	@Override
	protected void initView() {
		//关闭加载更多
		setCanLoadMore(true);

		listLiew.setOnItemClickListener(this);
	}

	@Override
	protected void initData() {
		clean();
		initRequest(HandlerType.loading);
	}

	@Override
	protected void initRequest(HandlerType handlerType) {

		//下拉刷新操作恢复初始状态
		if (HandlerType.refresh.equals(handlerType)) {
			clean();
		}

		//网络请求
		RequestParams params = new RequestParams();
		params.put("service_type", serviceType);

		if (HandlerType.more.equals(handlerType)) {
			params.put("page", page + 1);
		} else {
			params.put("page", page);
		}

		ApiRequest.homeRoomService.request(this, params, handlerType);
	}

	public ListView getListView() {
		return listLiew;
	}

	public void setBaseAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}

	public BaseAdapter getBaseAdapter() {
		return adapter;
	}

	public int getServiceType() {
		return serviceType;
	}

	public List<HomeRoomServiceItem> getList() {
		return list;
	}

	protected void clean() {
		list.clear();
		page = 1;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		HomeRoomServiceItem item = list.get(position);
		HomeServiceInfoFragment.go(getActivity(), item.getServiceItemId(), item.getServiceType(), item.getName());

	}

}
